home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
MacFormat España 26
/
macformat_26.iso
/
Shareware
/
Programación
/
CDEF Set #1
/
Extras
/
Demo App Project Files
/
CDEF Demo App.c
< prev
next >
Wrap
Text File
|
1996-11-30
|
16KB
|
633 lines
#ifndef __CDEFDEMOAPP__
#include "CDEF Demo App.h" // include all constant values from external file
#endif
#ifndef __GLOBALS__
#include "Globals.h"
#endif
#ifndef __FXNPROTOS__
#include "FxnProtos.h"
#endif
Boolean gDone = false;
/************************************************************************/
/* main */
/* */
/* Author: Jason Giles */
/* Created: September 16, 1995 */
/* Modified: February 2, 1996 */
/************************************************************************/
void main ( void )
{
ToolBoxInit ();
MenuBarInit ();
EventInit ();
EventLoop ();
}
#pragma mark -
/************************************************************************/
/* ToolBoxInit */
/* */
/* Purpose: Initializes all toolbox managers, in the most appropriate */
/* order according to various sources. */
/* Author: Jason Giles */
/* Created: September 16, 1995 */
/* Modified: October 15, 1995 */
/************************************************************************/
void ToolBoxInit ()
{
InitGraf (&qd.thePort);
InitFonts ();
InitWindows ();
InitMenus ();
TEInit ();
InitDialogs ( nil );
InitCursor ();
}
/************************************************************************/
/* EventInit */
/* */
/* Purpose: Initializes the Event Handler */
/* Comment: The err statements would not compile, so they were pre- */
/* processed out. */
/* Author: Jason Giles */
/* Created: September 16, 1995 */
/* Modified: August 7, 1996 */
/************************************************************************/
void EventInit ( void )
{
OSErr err;
AEEventHandlerUPP AEOpenApplicationUPP, AEOpenDocumentsUPP,
AEPrintDocumentsUPP, AEQuitApplicationUPP;
long feature;
err = Gestalt ( gestaltAppleEventsAttr, &feature );
if ( err != noErr )
{
DebugStr ( "\pProblem in calling Gestalt!" );
return;
}
else
{
if ( !( feature & ( kGestaltMask << gestaltAppleEventsPresent ) ) )
{
DebugStr ( "\pApple events not available" );
return;
}
}
// Install the OpenApp AppleEvent Handler.
AEOpenApplicationUPP = NewAEEventHandlerProc( AEOpenApplication );
err = AEInstallEventHandler( kCoreEventClass, kAEOpenApplication,
AEOpenApplicationUPP, 0, false );
if ( err != noErr ) DebugStr ("\pkAEOpenApplication Apple event not available!" );
// Install the OpenDoc AppleEvent Handler.
AEOpenDocumentsUPP = NewAEEventHandlerProc( AEOpenDocuments );
err = AEInstallEventHandler( kCoreEventClass, kAEOpenDocuments,
AEOpenDocumentsUPP, 0, false );
if ( err != noErr ) DebugStr ("\pkAEOpenDocuments Apple event not available!" );
// Install the PrintDoc AppleEvent Handler
AEPrintDocumentsUPP = NewAEEventHandlerProc( AEPrintDocuments );
err = AEInstallEventHandler( kCoreEventClass, kAEPrintDocuments,
AEPrintDocumentsUPP, 0, false );
if ( err != noErr ) DebugStr ("\pkAEPrintDocuments Apple event not available!" );
// Install the QuitApp AppleEvent Handler.
AEQuitApplicationUPP = NewAEEventHandlerProc( AEQuitApplication );
err = AEInstallEventHandler( kCoreEventClass, kAEQuitApplication,
AEQuitApplicationUPP, 0, false );
if ( err != noErr ) DebugStr ("\pkAEQuitApplication Apple event not available!" );
}
/************************************************************************/
/* MenuBarInit */
/* */
/* Purpose: Initializes the Menu Bar */
/* Comment: */
/* Author: Jason Giles */
/* Created: September 16, 1995 */
/* Modified: January 15, 1995 */
/************************************************************************/
void MenuBarInit ( void )
{
Handle menuBar; // handle for the menubar
MenuHandle menu;
menuBar = GetNewMBar ( kBaseResID ); // assign a new menu bar to the menuBar handle
SetMenuBar ( menuBar ); // pick the menubar to create
menu = GetMHandle ( 128 ); // get apple menu
AddResMenu ( menu, 'DRVR' );
menu = GetMHandle ( 129 ); // get file menu
DrawMenuBar (); // show the menubar
}
#pragma mark -
/************************************************************************/
/* EventLoop */
/* */
/* Author: Jason Giles */
/* Created: September 16, 1995 */
/* Modified: January 15, 1995 */
/************************************************************************/
void EventLoop ( void )
{
EventRecord event;
gDone = false;
while ( gDone == false )
{
WaitNextEvent ( everyEvent, &event, kSleep, nil ); // get next event in the que
DoDialogEvent ( &event ); // DoDialogEvent function, then
}
}
/************************************************************************/
/* DoDialogEvent */
/* Author: Jason Giles */
/* Created: September 16, 1995 */
/* Modified: January 15, 1995 */
/************************************************************************/
void DoDialogEvent ( EventRecord *eventPtr )
{
char theChar;
short itemHit = 0;
switch ( eventPtr->what )
{
case kHighLevelEvent:
AEProcessAppleEvent ( eventPtr );
break;
case mouseDown:
HandleMouseDown ( eventPtr, itemHit );
break;
case mouseUp:
break;
case keyUp:
break;
case keyDown:
case autoKey:
theChar = eventPtr->message & charCodeMask;
if ( (eventPtr->modifiers & cmdKey) != 0)
HandleMenuChoice ( MenuKey ( theChar ) );
break;
case updateEvt:
BeginUpdate ( (WindowPtr) eventPtr->message );
EndUpdate ( (WindowPtr) eventPtr->message );
break;
case diskEvt:
break;
case activateEvt:
break;
case networkEvt:
break;
case driverEvt:
break;
case app1Evt:
break;
case app2Evt:
break;
case app3Evt:
break;
case osEvt:
if ( (eventPtr->message & suspendResumeMessage) == resumeFlag )
break;
break;
default:
break;
}
}
#pragma mark -
/************** HandleMouseDown function **************/
void HandleMouseDown ( EventRecord *eventPtr, short itemHit )
{
WindowPtr whichWindow;
GrafPtr oldPort;
long menuChoice;
short thePart;
ControlHandle control;
short trigger = false;
MenuHandle menu;
short result;
thePart = FindWindow ( eventPtr -> where, &whichWindow );
switch ( thePart )
{
case inMenuBar:
menuChoice = MenuSelect ( eventPtr->where );
HandleMenuChoice ( menuChoice );
break;
case inSysWindow:
SystemClick ( eventPtr, whichWindow );
break;
}
}
/************** HandleMenuChoice function **************/
void HandleMenuChoice ( long menuChoice )
{
short menu;
short item;
if ( menuChoice != 0 )
{
menu = HiWord ( menuChoice );
item = LoWord ( menuChoice );
switch ( menu )
{
case 128:
HandleAppleChoice ( item );
break;
case 129:
HandleFileChoice ( item );
break;
}
HiliteMenu ( 0 );
}
}
/************** HandleAppleChoice function **************/
void HandleAppleChoice ( short item )
{
MenuHandle appleMenu;
Str255 accName;
short accNumber;
switch ( item )
{
case 1:
DrawAboutDialog ();
break;
default:
appleMenu = GetMHandle (128);
GetItem ( appleMenu, item, accName );
accNumber = OpenDeskAcc ( accName );
break;
}
}
/************** HandleFileChoice function **************/
void HandleFileChoice ( short item )
{
WindowPtr window = nil;
MenuHandle menu;
short result = 2;
Str63 untitledWindow = "\puntitled";
window = FrontWindow ();
switch ( item )
{
case 1:
HandleTest1 ();
break;
case 2:
HandleTest2 ();
break;
case 4:
gDone = true;
break;
}
}
#pragma mark -
/************** DrawAboutDialog function **************/
void DrawAboutDialog ( void )
{
DialogRecord myDialog;
DialogPtr dialog;
Boolean dialogDone = false;
short itemType, item1;
Rect itemRect;
Handle textItemHandle;
Handle textItemHandle2;
dialog = GetNewDialog ( 128, nil, kMoveToFront );
if ( dialog )
{
SetPort ( dialog );
ShowWindow ( dialog );
do
{
ModalDialog ( NULL, &item1 );
} while ( item1 != 1 );
DisposeDialog ( dialog );
}
}
/************************************************************************/
/* HandleTmCalcParametersChoice function */
/* */
/* This copies the value of the input variables into the text edit box */
/* Author: Jason Giles & Ray Lefebvre */
/* Created: September 16, 1995 */
/* Modified: April 4, 1996 */
/************************************************************************/
void HandleTest1 ()
{
DialogRecord myDialog;
DialogPtr dialog;
Boolean dialogDone = false;
short itemHit = 0, itemType = 0;
Handle okItemHandle;
Handle cancelItemHandle;
Rect itemRect;
Handle itemHandle;
dialog = GetNewDialog ( 130, nil, kMoveToFront );
SetPort ( dialog );
SetDialogFontAndSize( dialog, geneva, 9 );
SetDialogDefaultItem ( dialog, ok ); // this draws circle around default item
SetDialogTracksCursor ( dialog, true );
GetDItem ( dialog, 3, &itemType, &itemHandle, &itemRect );
SetCtlValue ( (ControlHandle) itemHandle, 1 );
GetDItem ( dialog, 4, &itemType, &itemHandle, &itemRect );
SetCtlValue ( (ControlHandle) itemHandle, 0);
GetDItem ( dialog, 5, &itemType, &itemHandle, &itemRect );
SetCtlValue ( (ControlHandle) itemHandle, 0 );
GetDItem ( dialog, 6, &itemType, &itemHandle, &itemRect );
SetCtlValue ( (ControlHandle) itemHandle, 0 );
HideDItem ( dialog, 14 );
HideDItem ( dialog, 15 );
HideDItem ( dialog, 16 );
HideDItem ( dialog, 17 );
HideDItem ( dialog, 18 );
HideDItem ( dialog, 19 );
HideDItem ( dialog, 20 );
HideDItem ( dialog, 21 );
HideDItem ( dialog, 22 );
HideDItem ( dialog, 23 );
HideDItem ( dialog, 2 );
ShowDItem ( dialog, 8 );
ShowDItem ( dialog, 9 );
ShowDItem ( dialog, 10 );
ShowDItem ( dialog, 11 );
ShowDItem ( dialog, 12 );
ShowDItem ( dialog, 13 );
ShowWindow ( dialog );
while ( !dialogDone )
{
ModalDialog ( nil, &itemHit );
switch ( itemHit )
{
case ( 3 ):
GetDItem ( dialog, 3, &itemType, &itemHandle, &itemRect );
SetCtlValue ( (ControlHandle) itemHandle, 1 );
GetDItem ( dialog, 4, &itemType, &itemHandle, &itemRect );
SetCtlValue ( (ControlHandle) itemHandle, 0 );
GetDItem ( dialog, 5, &itemType, &itemHandle, &itemRect );
SetCtlValue ( (ControlHandle) itemHandle, 0 );
GetDItem ( dialog, 6, &itemType, &itemHandle, &itemRect );
SetCtlValue ( (ControlHandle) itemHandle, 0 );
HideDItem ( dialog, 14 );
HideDItem ( dialog, 15 );
HideDItem ( dialog, 16 );
HideDItem ( dialog, 17 );
HideDItem ( dialog, 18 );
HideDItem ( dialog, 19 );
HideDItem ( dialog, 20 );
HideDItem ( dialog, 21 );
HideDItem ( dialog, 22 );
HideDItem ( dialog, 23 );
HideDItem ( dialog, 2 );
ShowDItem ( dialog, 8 );
ShowDItem ( dialog, 9 );
ShowDItem ( dialog, 10 );
ShowDItem ( dialog, 11 );
ShowDItem ( dialog, 12 );
ShowDItem ( dialog, 13 );
break;
case ( 4 ):
GetDItem ( dialog, 3, &itemType, &itemHandle, &itemRect );
SetCtlValue ( (ControlHandle) itemHandle, 0 );
GetDItem ( dialog, 4, &itemType, &itemHandle, &itemRect );
SetCtlValue ( (ControlHandle) itemHandle, 1 );
GetDItem ( dialog, 5, &itemType, &itemHandle, &itemRect );
SetCtlValue ( (ControlHandle) itemHandle, 0 );
GetDItem ( dialog, 6, &itemType, &itemHandle, &itemRect );
SetCtlValue ( (ControlHandle) itemHandle, 0 );
HideDItem ( dialog, 8 );
HideDItem ( dialog, 9 );
HideDItem ( dialog, 10 );
HideDItem ( dialog, 11 );
HideDItem ( dialog, 12 );
HideDItem ( dialog, 13 );
HideDItem ( dialog, 20 );
HideDItem ( dialog, 21 );
HideDItem ( dialog, 22 );
HideDItem ( dialog, 23 );
HideDItem ( dialog, 2 );
ShowDItem ( dialog, 14 );
ShowDItem ( dialog, 15 );
ShowDItem ( dialog, 16 );
ShowDItem ( dialog, 17 );
ShowDItem ( dialog, 18 );
ShowDItem ( dialog, 19 );
break;
case ( 5 ):
GetDItem ( dialog, 3, &itemType, &itemHandle, &itemRect );
SetCtlValue ( (ControlHandle) itemHandle, 0 );
GetDItem ( dialog, 4, &itemType, &itemHandle, &itemRect );
SetCtlValue ( (ControlHandle) itemHandle, 0 );
GetDItem ( dialog, 5, &itemType, &itemHandle, &itemRect );
SetCtlValue ( (ControlHandle) itemHandle, 1 );
GetDItem ( dialog, 6, &itemType, &itemHandle, &itemRect );
SetCtlValue ( (ControlHandle) itemHandle, 0 );
HideDItem ( dialog, 14 );
HideDItem ( dialog, 15 );
HideDItem ( dialog, 16 );
HideDItem ( dialog, 17 );
HideDItem ( dialog, 18 );
HideDItem ( dialog, 19 );
HideDItem ( dialog, 23 );
HideDItem ( dialog, 2 );
HideDItem ( dialog, 8 );
HideDItem ( dialog, 9 );
HideDItem ( dialog, 10 );
HideDItem ( dialog, 11 );
HideDItem ( dialog, 12 );
HideDItem ( dialog, 13 );
ShowDItem ( dialog, 20 );
ShowDItem ( dialog, 21 );
ShowDItem ( dialog, 22 );
break;
case ( 6 ):
GetDItem ( dialog, 3, &itemType, &itemHandle, &itemRect );
SetCtlValue ( (ControlHandle) itemHandle, 0 );
GetDItem ( dialog, 4, &itemType, &itemHandle, &itemRect );
SetCtlValue ( (ControlHandle) itemHandle, 0 );
GetDItem ( dialog, 5, &itemType, &itemHandle, &itemRect );
SetCtlValue ( (ControlHandle) itemHandle, 0 );
GetDItem ( dialog, 6, &itemType, &itemHandle, &itemRect );
SetCtlValue ( (ControlHandle) itemHandle, 1 );
HideDItem ( dialog, 8 );
HideDItem ( dialog, 9 );
HideDItem ( dialog, 10 );
HideDItem ( dialog, 11 );
HideDItem ( dialog, 12 );
HideDItem ( dialog, 13 );
HideDItem ( dialog, 14 );
HideDItem ( dialog, 15 );
HideDItem ( dialog, 16 );
HideDItem ( dialog, 17 );
HideDItem ( dialog, 18 );
HideDItem ( dialog, 19 );
HideDItem ( dialog, 20 );
HideDItem ( dialog, 21 );
HideDItem ( dialog, 22 );
ShowDItem ( dialog, 23 );
ShowDItem ( dialog, 2 );
break;
case ok:
dialogDone = true;
break;
}
}
DisposeDialog ( dialog );
}
/************************************************************************/
/* HandleTmCalcParametersChoice function */
/* */
/* This copies the value of the input variables into the text edit box */
/* Author: Jason Giles & Ray Lefebvre */
/* Created: September 16, 1995 */
/* Modified: April 4, 1996 */
/************************************************************************/
void HandleTest2 ( void )
{
DialogRecord myDialog;
DialogPtr dialog;
Boolean dialogDone = false;
short itemHit = 0, itemType = 0;
Handle okItemHandle;
Handle cancelItemHandle;
Rect itemRect;
Handle itemHandle;
dialog = GetNewDialog ( 129, nil, kMoveToFront );
SetPort ( dialog );
SetDialogDefaultItem ( dialog, ok ); // this draws circle around default item
SetDialogTracksCursor ( dialog, true );
ShowWindow ( dialog );
while ( !dialogDone )
{
ModalDialog ( nil, &itemHit );
switch ( itemHit )
{
case ok:
dialogDone = true;
break;
}
}
DisposeDialog ( dialog );
}
/***************************************************************************************************
GetDialogItemHandle
by Bob Bradley
***************************************************************************************************/
void GetDialogItemHandle( DialogRef dialog, short item, Handle *iHandle )
{
short iType;
Rect iRect;
GetDialogItem( dialog, item, &iType, iHandle, &iRect );
}
/************ SetDialogFontAndSize Function ************/
void SetDialogFontAndSize( DialogRef dialog, short font, short fontSize )
{
FontInfo fInfo;
TextFont( font );
TextSize( fontSize );
GetFontInfo( &fInfo );
( **( ( ( DialogPeek ) dialog )->textH ) ).txFont = font;
( **( ( ( DialogPeek ) dialog )->textH ) ).txSize = fontSize;
( **( ( ( DialogPeek ) dialog )->textH ) ).lineHeight = fInfo.ascent + fInfo.descent +
fInfo.leading;
( **( ( ( DialogPeek ) dialog )->textH ) ).fontAscent = fInfo.ascent;
}